// B - Input Data; N - number of elements in B
// A - RMQ array; K - number of elements in A

const 
  BAD : longint = round(1e9); // INF

function minI(x, y : longint) : longint; // gets indexes of elements, returns index of minimal element
begin
	if b[x] < b[y] then result := x else result := y;
end;

function build(n : longint) : longint; // builds A and  returns K?

var                                
	k, kk, i, j : longint;
	
begin
	k := 1;
	while k <= n do 
		k := k shl 1;
	kk := k;
	k := k shl 1;
	b[0] := bad;
	for i := n + 1 to kk do 
		b[i] := bad;
	j := 1;
	for i := kk to k do begin
		a[i] := j;
		inc(j);
	end;
	for i	:=	kk - 1 downto 1 do
		a[i] := minI(a[i shl 1], a[i shl 1 + 1]);
	result := k;
end;

function rmq(l, r : longint) : longint; // gives indexes, returns index

var
	kk : longint;
	
begin
	kk := k shr 1;
	inc(l, kk - 1);
	inc(r, kk - 1);
	result := minI(a[l], a[r]);
	while r - l > 1 do begin 
		if not odd(l) then result := minI(result, a[l + 1]);
		if odd(r) then result := minI(result, a[r - 1]);
		l := l shr 1;
		r := r shr 1;	
	end;
end;